package net.wanho.controller;

import lombok.SneakyThrows;
import net.wanho.po.Account;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-09-16 9:17
 * Description：<描述>
 */
@RestController
@RequestMapping("/param")
public class ParamController {

    /**
     * JavaEE组件
     */
    @SneakyThrows
    @RequestMapping("/param1")
    public void param1(HttpServletRequest req, HttpServletResponse resp){
        System.out.println(req.getParameter("name"));
        System.out.println(req.getHeader("User-Agent"));

        resp.setContentType("text/html; charset=utf8");
        PrintWriter out = resp.getWriter();
        out.print("<h2>成功</h2>");
        out.close();
    }

    /**
     * String、基本类型及包装类
     */
    @RequestMapping("/param2")
    public AjaxResult param2(@RequestParam("username") String name, @RequestParam(required = false, defaultValue = "18") int age, Double height){
        System.out.println("姓名：" + name);
        System.out.println("年龄：" + age);
        System.out.println("身高：" + height);
        return AjaxResult.success("成功");
    }

    @RequestMapping("/param3/{id}/{name}")
    public AjaxResult param3(@PathVariable("id") int id, @PathVariable String name){
        System.out.println("id：" + id);
        System.out.println("name：" + name);
        return AjaxResult.success("成功");
    }

    @RequestMapping("/param4")
    public AjaxResult param4(@RequestHeader("User-Agent") String userAgent){
        System.out.println("User-Agent：" + userAgent);
        return AjaxResult.success("成功");
    }

    @RequestMapping("/param5")
    public AjaxResult param5(@CookieValue("address") String address){
        System.out.println("address：" + address);
        return AjaxResult.success("成功");
    }

    @RequestMapping("/param6")
    // public AjaxResult param6(@RequestBody String args){
    // public AjaxResult param6(@RequestBody Account account){
    public AjaxResult param6(@RequestBody List<Integer> ids){
        // System.out.println("args：" + args);
        // System.out.println("account：" + account);
        System.out.println("ids：" + ids);

        return AjaxResult.success("成功");
    }

    /**
     * 自定义对象
     */
    @RequestMapping("/param7")
    public AjaxResult param7(Account account){
        System.out.println("account：" + account);
        return AjaxResult.success("成功");
    }

    /**
     * 限定请求方式
     */
    // @RequestMapping
    @GetMapping("/param8")
    public AjaxResult param8(){
        return AjaxResult.success("成功");
    }
}
